package com.woniuxy.service.Impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.woniuxy.dao.AssetDao;
import com.woniuxy.dao.LoanUserDao;
import com.woniuxy.entity.AssetInfo;
import com.woniuxy.service.AssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName AssetServiceImpl
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/9/20 15:17
 * @Version 1.0
 */
@Service
public class AssetServiceImpl implements AssetService {
    @Autowired
    AssetDao assetDao;
    @Autowired
    LoanUserDao loanUserDao;

    /**
     * 添加资产信息
     * @param assetInfo
     * @return
     */
    @Override
    public AjaxResult addAsset(AssetInfo assetInfo) {
        int userId = Math.toIntExact(SecurityUtils.getUserId());
        //查询当前用户是否存在信息，存在就更改，不存在就新增
        Integer id = loanUserDao.findLoanUserCount(userId);
        if(id!=null){
            int i= assetDao.updateAsset(assetInfo, id);
            if(i>0){
                return new AjaxResult(200,"success");
            }else{
                return new AjaxResult(500,"defeated");
            }
        }else{
            int i= assetDao.addAsset(assetInfo);
            if(i>0){
                return new AjaxResult(200,"success");
            }else{
                return new AjaxResult(500,"defeated");
            }
        }
    }
}
